package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.page.PageDto;
import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.AwardProductDto;
import com.xiaoc.bulldozer.common.dto.AwardProductQueryDto;
import com.xiaoc.bulldozer.common.model.AwardProduct;
import com.xiaoc.bulldozer.common.service.AwardProductService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/awardProduct")
public class AwardProductController extends AdminBaseController {

    public static final Logger logger = LoggerFactory.getLogger(AwardProductController.class);

    @Resource(name = "awardProductService")
    private AwardProductService awardProductService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(AwardProductQueryDto queryDto) {

        if(queryDto.getPage() == null) queryDto.setPage(1);
//        queryDto.setTitle(StringUtils.trimToNull(queryDto.getTitle()));
        
        int count = awardProductService.getAwardProductCountByCondition(queryDto);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageDto", new PageDto(queryDto.getPage(), count, PageDto.PAGE_SIZE));

        if (count == 0) {
            params.put("queryDto", queryDto);
            return renderHtml("awardProduct/list.html", params);
        }
        
        List<AwardProduct> awardProductList = awardProductService.getAwardProductListByCondition(queryDto);
        params.put("awardProductList", awardProductList);
        params.put("queryDto", queryDto);
        return renderHtml("awardProduct/list.html", params);
    }

    
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView detail(Integer id) {
        AwardProduct awardProduct = null;
        if(id != null) {
            awardProduct = awardProductService.getAwardProductById(id);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("awardProduct", awardProduct);
        return renderHtml("awardProduct/edit.html", params);
    }

    
    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST)
    public ModelAndView createOrUpdate(@RequestBody AwardProductDto awardProductDto) {
        
        if (StringUtils.isBlank(awardProductDto.getName())) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请红包活动名称");
        }

        if (awardProductDto.getFee() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写金额");
        }
        
        if (awardProductDto.getValidCount() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写有效数量");
        }
        
        if (awardProductDto.getUpperLimitRate() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写红包金额比率上限");
        }
        
        if (awardProductDto.getLowerLimitRate() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写红包金额比率下限");
        }
        
        if (awardProductDto.getStatus() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请选择红包活动状态");
        }
        
        if (awardProductDto.getLikeCount() == null) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写点赞人数");
        }
        

        long currentTime = System.currentTimeMillis();
        AwardProduct awardProduct = BeanUtil.copyProperties(AwardProduct.class, awardProductDto);
        awardProduct.setUpdateTime(currentTime);
        awardProduct.setCreateTime(currentTime);

        try {
            if (awardProduct.getId() == null) {
                awardProductService.save(awardProduct);
            } else {
                awardProductService.update(awardProduct);
            }
        } catch (Exception e) {
            logger.error("createOrUpdate failed", e);
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "操作失败");
        }
        return jsonSucess();
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestBody AwardProductDto awardProductDto) {
        if (awardProductDto.getId() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "该红包活动不存在");
        }
        awardProductService.deleteById(awardProductDto.getId());
        return jsonSucess();
    }

}
